package part_10_Re_entrent_Locks;

import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Runner {

	private int count = 0;
	private Lock l = new ReentrantLock();
	private Condition cond = l.newCondition();

	void increment() {

		for (int i = 0; i < 10000; i++) {
			count++;
		}
	}

	public void firstThread() throws InterruptedException {
		l.lock();
		System.out.println("Waiting...");
		cond.await();
		System.out.println("Woken Up");

		try {
			System.out.println("in the 1st increment");
			increment();
		} finally {
			l.unlock();
		}

	}

	public void secondThread() throws InterruptedException {
		Thread.sleep(1000);
		l.lock();

		System.out.println("Please Press the Enter Key");
		new Scanner(System.in).nextLine();
		System.out.println("Pressed the Enter key");

		cond.signal();
		
		try {
			System.out.println("in the 2nd increment");
			increment();

		} finally {
			l.unlock();
		}

	}

	public void finished() {
		System.out.println("count :: " + count);

	}

}
